#Democratic Reputations in Crisis and War: replication data
#November 20, 2021
#Josh Kertzer

library(here)
library(mediation)
library(stargazer)
library(parallel)
library(ggplot2)
library(ggridges)
library(tidyr)
library(haven)
library(ggmosaic)
library(DirectEffects)


######### Block A Replication I 112021.R: this R file contains the code necessary to load the datasets and generate covariates for the analysis - run this file first!
# All of the following analyses were carried out using R version 4.1.2 GUI 1.77 (Big Sur ARM build 8007) on an M1 Pro Macbook Pro running MacOS Monterey. 

#Function to recode our variables
recode <- function(variable, reverse=FALSE, maxVal, minVal, binarize=FALSE, x=NA, x2){
	if (is.factor(variable)){variable <- as.numeric(variable)}
	if (missing(maxVal)){maxVal <- max(variable, na.rm=TRUE)}
	if (missing(minVal)){minVal <- min(variable, na.rm=TRUE)}
	variable[variable > maxVal] <- NA
	variable[variable < minVal] <- NA
	if (reverse){
		temp <- variable
		for (j in 1:maxVal){
			temp[which(variable==j)] <- maxVal-j+1
		}
		return(temp)
	}
	if (binarize){
		if (is.na(x)){stop("Specify the value to dichotomize on")}
		temp <- variable
		if (missing(x2)){
			i <- which(variable == x)
			k <- which(variable < x | variable > x)
		}
		else{
			i <- which(variable >= x & variable <= x2)
			k <- which(variable < x | variable > x2)
		}
		temp[i] <- 1
		temp[k] <- 0
		return(temp)
	}
	return(variable)
}

rescale <- function(x){
	return((x-min(x,na.rm=TRUE))/(max(x-min(x,na.rm=TRUE),na.rm=TRUE)))
}


#### Israeli Knesset data
knes <- get(load("Block A Israel Elite.RData"))

## Generate individual differences
#Militant assertiveness

ma1 <- recode(as.numeric(knes$ma1), reverse=TRUE)
ma2 <- as.numeric(knes$ma2)
ma3 <- recode(as.numeric(knes$ma3), reverse=TRUE)
knes$milAssert1 <- rescale(apply(cbind(ma1, ma2, ma3), 1,sum,na.rm=TRUE)) #Ranges from 0-1
knes$milAssert2 <- recode(knes$milAssert1, binarize=TRUE, x=mean(knes$milAssert1, na.rm=TRUE), x2=1) #Mean-split

#International trust
knes$intTrust1 <- rescale(recode(as.numeric(knes$intTrust_1), reverse=TRUE))

#Ideology
knes$ideo1 <- rescale(recode(as.numeric(knes$ideo_1), reverse=TRUE))

#Arab-Israeli hawkishness
knes$hawk <- rescale(recode(as.numeric(knes$IsrArab_1), reverse=TRUE))

### Generate treatment
knes$A <- recode(knes$RegimeA,binarize=TRUE, x=2) #Democracy = 1, Dictatorship = 0


## Primary outcomes
knes$DV_A1 <- as.numeric(knes$outcmA1_1) #Crisis
knes$DV_A2 <- as.numeric(knes$outcmA2_1) #War

#### Israeli Public I data
isr1 <- get(load("Block A Israel Public I.RData"))

## Education
isr1$educ1 <- rescale(isr1$educ)
isr1$educCat <- isr1$educ
isr1$educCat[which(isr1$educCat==6)] <- 5 #Combine MA and PhD to make things more comparable with other samples

isr1$educCat4 <- isr1$educCat #Four category version (more easily compare with UK sample)
isr1$educCat4[which(isr1$educCat <=2)] <- 1
isr1$educCat4[which(isr1$educCat == 3)] <- 2
isr1$educCat4[which(isr1$educCat == 4)] <- 3
isr1$educCat4[which(isr1$educCat == 5)] <- 4 

## Gender
isr1$Male <- recode(isr1$gender, binarize=TRUE, x=1)

## Knowledge
k1 <- recode(as.numeric(isr1$k1),binarize=TRUE, x=7)
k2 <- recode(as.numeric(isr1$k2),binarize=TRUE, x=4)
k3 <- recode(as.numeric(isr1$k3),binarize=TRUE, x=6) #Appointment of judges to supreme court
k4 <- recode(as.numeric(isr1$k4),binarize=TRUE, x=5) #Golan heights conquered in six day war
k5 <- recode(as.numeric(isr1$k5),binarize=TRUE, x=4) #First country to sign peace deal with Israel
isr1$know1 <- rescale(apply(cbind(k1,k2,k3,k4,k5),1,sum))

## Combat experience
isr1$combat <- recode(as.numeric(isr1$army), binarize=TRUE,x=3) #289 saw combat experience

## Military assertiveness
ma1 <- recode(as.numeric(isr1$ma1), reverse=TRUE)
ma2 <- as.numeric(isr1$ma2)
ma3 <- recode(as.numeric(isr1$ma3), reverse=TRUE)
isr1$milAssert1 <- rescale(apply(cbind(ma1, ma2, ma3), 1,sum)) #Ranges from 0-1

## Immigration status
isr1$isrlBrn <- recode(as.numeric(isr1$brthCtry),binarize=TRUE,x=4)
isr1$USSRBrn <- recode(as.numeric(isr1$brthCtry), binarize=TRUE, x=5)
isr1$isrlUSSR <- NA #Compare born in Israel (0) to born in the USSR (1)
isr1$isrlUSSR[which(isr1$isrlBrn==1)] <- 0
isr1$isrlUSSR[which(isr1$USSRBrn==1)] <- 1

## International trust
isr1$intTrust1 <- rescale(recode(as.numeric(isr1$intTrust_1), reverse=TRUE))

#Ideology
isr1$ideo1 <- rescale(recode(as.numeric(isr1$ideo_1), reverse=TRUE))

#Arab-Israeli hawkishness
isr1$hawk <- rescale(recode(as.numeric(isr1$IsrArab_1), reverse=TRUE))

#Age in categorical form
isr1$ageCat <- NA
isr1$ageCat[which(isr1$age < 25)] <- 1
isr1$ageCat[which(isr1$age >= 25 & isr1$age <=34)] <- 2
isr1$ageCat[which(isr1$age >= 35 & isr1$age <=44)] <- 3
isr1$ageCat[which(isr1$age >= 45 & isr1$age <=54)] <- 4
isr1$ageCat[which(isr1$age >= 55)] <- 5

#Arab-Israeli hawkishness
isr1$hawk <- rescale(recode(as.numeric(isr1$IsrArab_1), reverse=TRUE))

### Generate treatment
isr1$A <- recode(isr1$RegimeA, maxVal=2, binarize=TRUE, x=2) #Democracy = 1, Dictatorship = 0

## Primary outcomes
isr1$DV_A1 <- as.numeric(isr1$outcmA1_1) #Crisis
isr1$DV_A2 <- as.numeric(isr1$outcmA2_1) #War

#### Israeli Public II
isr2 <- get(load("Block A Israel Public II.RData"))

#Generate individual differences

isr2$ageCat <- NA
isr2$ageCat[which(isr2$age < 25)] <- 1
isr2$ageCat[which(isr2$age >= 25 & isr2$age <=34)] <- 2
isr2$ageCat[which(isr2$age >= 35 & isr2$age <=44)] <- 3
isr2$ageCat[which(isr2$age >= 45 & isr2$age <=54)] <- 4
isr2$ageCat[which(isr2$age >= 55)] <- 5

## Education
isr2$educ1 <- rescale(isr2$educ)
isr2$educCat <- isr2$educ
isr2$educCat[which(isr2$educCat==6)] <- 5 #Combine MA and PhD to make things more comparable with other samples

isr2$educCat4 <- isr2$educCat #Four category version (more easily compare UK sample)
isr2$educCat4[which(isr2$educCat <=2)] <- 1
isr2$educCat4[which(isr2$educCat == 3)] <- 2
isr2$educCat4[which(isr2$educCat == 4)] <- 3
isr2$educCat4[which(isr2$educCat == 5)] <- 4 

## Gender
isr2$Male <- recode(isr2$gender, binarize=TRUE, x=1)

## Knowledge
k1 <- recode(as.numeric(isr2$k1),binarize=TRUE, x=7)
k2 <- recode(as.numeric(isr2$k2),binarize=TRUE, x=4)
k3 <- recode(as.numeric(isr2$k3),binarize=TRUE, x=6) #Appointment of judges to supreme court
k4 <- recode(as.numeric(isr2$k4),binarize=TRUE, x=5) #Golan heights conquered in six day war
k5 <- recode(as.numeric(isr2$k5),binarize=TRUE, x=4) #First country to sign peace deal with Israel
isr2$know1 <- rescale(apply(cbind(k1,k2,k3,k4,k5),1,sum))

## Combat experience
isr2$combat <- recode(as.numeric(isr2$army), binarize=TRUE,x=3) #289 saw combat experience

## Military assertiveness
ma1 <- recode(as.numeric(isr2$ma1), reverse=TRUE)
ma2 <- as.numeric(isr2$ma2)
ma3 <- recode(as.numeric(isr2$ma3), reverse=TRUE)
isr2$milAssert1 <- rescale(apply(cbind(ma1, ma2, ma3), 1,sum)) #Ranges from 0-1

## Immigration status
isr2$isrlBrn <- recode(as.numeric(isr2$brthCtry),binarize=TRUE,x=4)
isr2$USSRBrn <- recode(as.numeric(isr2$brthCtry), binarize=TRUE, x=5)
isr2$isrlUSSR <- NA #Compare born in Israel (0) with born in the USSR (1)
isr2$isrlUSSR[which(isr2$isrlBrn==1)] <- 0
isr2$isrlUSSR[which(isr2$USSRBrn==1)] <- 1

#Ideology
isr2$ideo1 <- rescale(recode(as.numeric(isr2$ideo_1), reverse=TRUE))

#Arab-Israeli hawkishness
isr2$hawk <- rescale(recode(as.numeric(isr2$IsrArab_1), reverse=TRUE))

### Generate treatment
isr2$A <- recode(isr2$RegimeA, binarize=TRUE, x=2) #Democracy = 1, Dictatorship = 0

## Primary outcomes
isr2$DV_A1 <- as.numeric(isr2$outcmA1_1) #Crisis
isr2$DV_A2 <- as.numeric(isr2$outcmA2_1) #War

## Secondary outcomes
isr2$publicPacifist <- as.numeric(isr2$gPacifist_1)
isr2$elitePacifist <- as.numeric(isr2$gPacifist_2)
isr2$casualtySensitive <- recode(as.numeric(isr2$gCost_1), reverse=TRUE)
isr2$finSensitive <- recode(as.numeric(isr2$gCost_2), reverse=TRUE)
isr2$pubSupportL <- recode(as.numeric(isr2$lPub_6), reverse=TRUE)
isr2$pubSupportW <- recode(as.numeric(isr2$wPub_6), reverse=TRUE)
isr2$pubSupportDiff <- isr2$pubSupportL - isr2$pubSupportW
isr2$turnoverL <- recode(as.numeric(isr2$lPub_7), reverse=TRUE)
isr2$turnoverW <- recode(as.numeric(isr2$wPub_7), reverse=TRUE)
isr2$turnoverDiff <- isr2$turnoverL - isr2$turnoverW
isr2$leaderFate <- recode(as.numeric(isr2$llFate), reverse=TRUE) #Higher values = better fate
isr2$followThrough <- as.numeric(isr2$gcLikely_1)
isr2$selectWin <- as.numeric(isr2$gcLikely2_3)
isr2$reliableAllies <- as.numeric(isr2$gcLikely2_4) #How likely other countries will come to A's defense?
isr2$training <- recode(as.numeric(isr2$gTrained), reverse=TRUE)
isr2$morale <- recode(as.numeric(isr2$gMorale), reverse=TRUE)

#### USA data
usa <- get(load("Block A USA.RData"))

#Treatments
usa$A <- NA
usa$A[which(usa$RegimeA=="Democracy")] <- 1
usa$A[which(usa$RegimeA=="Dictatorship")] <- 0

usa$DemocracyA <- recode(as.numeric(usa$RegimeA),binarize=TRUE,x=1)
usa$DictatorshipA <- recode(as.numeric(usa$RegimeA),binarize=TRUE,x=2)
usa$DemTransitionA <- recode(as.numeric(usa$RegimeA),binarize=TRUE,x=3)

#Primary outcomes
usa$DV_A1 <- as.numeric(usa$outcmA1_1)
usa$DV_A2 <- as.numeric(usa$outcmA3_1)

## Demographics
usa$age <- as.numeric(usa$age)

usa$ageCat <- NA
usa$ageCat[which(usa$age < 25)] <- 1
usa$ageCat[which(usa$age >= 25 & usa$age <= 34)] <- 2
usa$ageCat[which(usa$age >= 35 & usa$age <= 44)] <- 3
usa$ageCat[which(usa$age >= 45 & usa$age <= 54)] <- 4
usa$ageCat[which(usa$age >= 55)] <- 5

usa$Male <- recode(as.numeric(usa$gender), binarize=TRUE, x=1)
usa$educ <- as.numeric(usa$education)
usa$educ1 <- recode(usa$educ, binarize=TRUE, x=1) #Less than HS
usa$educ2 <- recode(usa$educ, binarize=TRUE, x=2) #High School grad
usa$educ3 <- recode(usa$educ, binarize=TRUE, x=3) #Some college
usa$educ4 <- recode(usa$educ, binarize=TRUE, x=4, x2=5) #College/University
usa$educ5 <- recode(usa$educ, binarize=TRUE, x=6, x2=8) #Post-grad

usa$educCat <- usa$educ1*1 + usa$educ2*2 + usa$educ3*3 + usa$educ4*4 + usa$educ5*5
usa$educCat4 <- (usa$educ1 + usa$educ2*1) + usa$educ3*2 + usa$educ4*3 + usa$educ5*4 #A four category version (makes more comparable with UK sample)

usa$pid <- recode(as.numeric(usa$party), reverse=TRUE)
usa$pid1 <- rescale(usa$pid) #Ranges from 0-1
usa$ideo <- recode(as.numeric(usa$ideology), reverse=TRUE)
usa$ideo1 <- rescale(usa$ideo) #Ranges from 0-1

usa$interestFA1 <- rescale(recode(as.numeric(usa$interest1), reverse=TRUE)) #Ranges from 0-1
usa$interestGov1 <- rescale(recode(as.numeric(usa$interest2), reverse=TRUE)) #Ranges from 0-1

usa$intTrust <- recode(as.numeric(usa$trust), binarize=TRUE, x=1)

ma1 <- recode(as.numeric(usa$MilAssert1), reverse=TRUE)
ma2 <- as.numeric(usa$MilAssert2)
ma3 <- recode(as.numeric(usa$MilAssert3), reverse=TRUE)
ma4 <- recode(as.numeric(usa$MilAssert4), reverse=TRUE)
usa$milAssert1 <- rescale(apply(cbind(ma1, ma2, ma3, ma4), 1,sum,na.rm=TRUE)) #Ranges from 0-1

#### UK data I
uk1 <- get(load("Block A UK I.RData"))

### Generate treatment
uk1$A <- NA
uk1$A[which(uk1$resolve1=="democracy")] <- 1 #Democracy
uk1$A[which(uk1$resolve1=="dictatorship")] <- 0 #Dictatorship

### Primary outcomes
uk1$DV_A1 <- as.numeric(uk1$Q6.a_1) #Crisis
uk1$DV_A2 <- as.numeric(uk1$Q6.b_1) #War

### Demographics (measured a bit differently than in Israel/USA)
uk1$ideo1 <- rescale(uk1$Q5.g_1)

uk1$ageCat <- uk1$Q5.b #< 25, 25-34 years old, 35-44 years old, 45-55 years old, > 25
uk1$ageCat[which(uk1$ageCat==7)] <- 5 #Ranges from 1-5

uk1$male <- recode(uk1$Q5.a, binarize=TRUE, x=1)

uk1$educCat <- uk1$Q5.c #Less than A level, A level or equivalent, HNC/HND, Univ/poly diploma or equivalent, First degree (BA, BSc), Postgraduate degree (MA, PhD, PGCE)
uk1$educCat4 <- uk1$educCat #Four category version (more easily compare with non-UK samples)
uk1$educCat4[which(uk1$educCat <=2)] <- 1
uk1$educCat4[which(uk1$educCat == 3)] <- 2
uk1$educCat4[which(uk1$educCat == 4)] <- 3
uk1$educCat4[which(uk1$educCat == 5)] <- 4 

uk1$interestGov1 <- rescale(uk1$Q5.l)

uk1$interestFP1 <- rescale(uk1$Q5.m)

#### UK data II
uk2 <- get(load("Block A UK II.RData"))

### Generate treatment
uk2$A <- NA
uk2$A[which(uk2$resolve1=="democracy")] <- 1 #Democracy
uk2$A[which(uk2$resolve1=="dictatorship")] <- 0 #Dictatorship

### Primary outcomes
uk2$DV_A1 <- as.numeric(uk2$Q6.a_1) #Crisis
uk2$DV_A2 <- as.numeric(uk2$Q6.b_1) #War

### Demographics (measured a bit differently than in Israel/USA)
uk2$ideo1 <- rescale(uk2$Q5.g_1)

uk2$ageCat <- uk2$Q5.b #< 25, 25-34 years old, 35-44 years old, 45-55 years old, > 25
uk2$ageCat[which(uk2$ageCat==7)] <- 5 #Ranges from 1-5

uk2$male <- recode(uk2$Q5.a, binarize=TRUE, x=1)

uk2$educCat <- uk2$Q5.c #Less than A level, A level or equivalent, HNC/HND, Univ/poly diploma or equivalent, First degree (BA, BSc), Postgraduate degree (MA, PhD, PGCE)
uk2$educCat4 <- uk2$educCat #Four category version (more easily compare with non-UK samples)
uk2$educCat4[which(uk2$educCat <=2)] <- 1
uk2$educCat4[which(uk2$educCat == 3)] <- 2
uk2$educCat4[which(uk2$educCat == 4)] <- 3
uk2$educCat4[which(uk2$educCat == 5)] <- 4 

uk2$interestGov1 <- rescale(uk2$Q5.l)

uk2$interestFP1 <- rescale(uk2$Q5.m)

#### ROK data
rok <- get(load("Block A ROK.RData"))

### Generate treatment
rok$A <- NA
rok$A[which(rok$Gubun_Q6==1)] <- 1 #Democracy
rok$A[which(rok$Gubun_Q6==2)] <- 0 #Dictatorship

### Primary outcomes
rok$DV_A1 <- as.numeric(rok$Q6a) #Crisis
rok$DV_A2 <- as.numeric(rok$Q6b) #War

### Demographics (measured a bit differently)
rok$ideo1 <- rescale(as.numeric(rok$Q5h))

rok$ageCat <- as.numeric(rok$SQ2_2) 
rok$ageCat[which(rok$ageCat==6)] <- 5 #Combine

rok$male <- as.numeric(recode(rok$SQ1, binarize=TRUE, x=1))

rok$educCat <- as.numeric(rok$Q5c)

rok$educCat4 <- rok$educCat #Four category version (more easily compare with UK sample)
rok$educCat4[which(rok$educCat <=2)] <- 1
rok$educCat4[which(rok$educCat == 3)] <- 2
rok$educCat4[which(rok$educCat == 4)] <- 3
rok$educCat4[which(rok$educCat == 5)] <- 4 

rok$interestGov1 <- rescale(as.numeric(rok$Q5j))

rok$interestFP1 <- rescale(as.numeric(rok$Q5k))

#### Load Downes and Sechser replication data
downes <- read_dta("Downes-Sechser-Appendix-B.dta")